/*
 * ServicoCorreio.java
 *
 * Created on 10/07/2007, 13:30:58
 *
 */
package br.com.grillo.jturbonote;

import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
import org.apache.commons.io.IOUtils;
import static br.com.grillo.jturbonote.Main.*;

/**
 *
 * @author grillo
 */
public class ServicoCorreio extends Thread {

    private Nota nota;
    private List<String> destino;
    private boolean sent;

    public ServicoCorreio(List<String> destino, Nota nota) {
        this.nota = nota;
        this.destino = destino;
    }

    @Override
    public void run() {
        OutputStream out;
        Socket s;
        for (String to : destino) {
            try {
                s = new Socket(to, ServidorJTurboNote.PORTO_PADRAO[0]);
                out = s.getOutputStream();
                IOUtils.write(nota.toTurboNote(), out);
                System.out.println(nota.toTurboNote());
                out.flush();
                Thread.sleep(500);
                out.close();
                s.close();
                sent = true;
            } catch (Exception ioEx) {
                logger.log(Level.SEVERE, "Erro ao enviar", ioEx);
            }
        }
    }

    public boolean isSent() {
        return sent;
    }
}
